package com.songqi.common.core.utils.file;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;



/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2023/2/19 18:25
 */
public class OfficeUtils {

    public static void genWord(Map<String,Object> dataMap, InputStream wordTemplate, FileOutputStream outputStream){
        try {
            XWPFTemplate template = XWPFTemplate.compile(wordTemplate).render(dataMap);
            template.write(outputStream);
            outputStream.flush();
            outputStream.close();
            template.close();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("生成失败");
        }
    }


    public static void genWord(Map<String,Object> dataMap, InputStream wordTemplate, String outFilePath){
        try {
            XWPFTemplate template = XWPFTemplate.compile(wordTemplate).render(dataMap);
            FileOutputStream outputStream = new FileOutputStream(outFilePath);
            template.write(outputStream);
            outputStream.flush();
            outputStream.close();
            template.close();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("word文件生成失败");
        }
    }


    public static Map<String, Object> selectWordRadio(Integer fieldValue, int fieldCount,  String fieldName, Map<String,Object> dataMap){
        // 字体Wingdings 2的符号
        TextRenderData selectSymbol = new TextRenderData("\uF0FE",new Style("Wingdings",10));
        TextRenderData unselectSymbol = new TextRenderData("\uF0A8",new Style("Wingdings",10));
        String itemName = "";
        for(int i = 0; i < fieldCount; i++){
            itemName = fieldName + i;
            if(fieldValue != null && i == fieldValue ){
                dataMap.put(itemName, selectSymbol);
            }else {
                dataMap.put(itemName, unselectSymbol);
            }
        }
        return dataMap;
    }




//
//    public static byte[] docxToPdf(InputStream src) {
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        byte[] resBytes = null;
//        String result;
//        try {
//            // pdf文件的尺寸
//            Document pdfDocument = new Document(PageSize.A3, 72, 72, 72, 72);
//            PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, baos);
//            XWPFDocument doc = new XWPFDocument(src);
//            pdfWriter.setInitialLeading(20);
//            List<XWPFParagraph> plist = doc.getParagraphs();
//            pdfWriter.open();
//            pdfDocument.open();
//            for (int i = 0; i < plist.size(); i++) {
//                XWPFParagraph pa = plist.get(i);
//                java.util.List<XWPFRun> runs = pa.getRuns();
//                for (int j = 0; j < runs.size(); j++) {
//                    XWPFRun run = runs.get(j);
//                    java.util.List<XWPFPicture> piclist = run.getEmbeddedPictures();
//                    Iterator<XWPFPicture> iterator = piclist.iterator();
//                    while (iterator.hasNext()) {
//                        XWPFPicture pic = iterator.next();
//                        XWPFPictureData picdata = pic.getPictureData();
//                        byte[] bytepic = picdata.getData();
//                        Image imag = Image.getInstance(bytepic);
//                        pdfDocument.add(imag);
//                    }
//                    // 中文字体的解决
//                    BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//                    Font font = new Font(bf, 11.0f, Font.NORMAL, BaseColor.BLACK);
//                    String text = run.getText(-1);
//                    byte[] bs;
//                    if (text != null) {
//                        bs = text.getBytes();
//                        String str = new String(bs);
//                        Chunk chObj1 = new Chunk(str, font);
//                        pdfDocument.add(chObj1);
//                    }
//                }
//                pdfDocument.add(new Chunk(Chunk.NEWLINE));
//            }
//            //需要关闭，不然无法获取到输出流
//            pdfDocument.close();
//            pdfWriter.close();
//            resBytes = baos.toByteArray();
//        } catch (Exception e) {
//            log.error("docx转pdf文件异常：{}",e);
//        }finally {
//            try{
//                if(baos != null){
//                    baos.close();
//                }
//            }catch (IOException e){
//                log.error("docx转pdf关闭io流异常：{}",e);
//            }
//        }
//        return resBytes;
//    }


    public static void main(String[] args) throws IOException {
//        String wordTemplate = "/Users/songqi/Desktop/demo.docx";
//        String outWordFilePath = "/Users/songqi/Desktop/file.docx";
//        Map<String,Object> dataMap = new HashMap<>();
//        dataMap.put("name", "宋祁");
//        dataMap.put("sex", "男");
//        dataMap.put("age", 39);
//        Style underLineStyle = new Style();
//        underLineStyle.setUnderLine(true);
//        dataMap.put("phone", new TextRenderData("13776346982", underLineStyle));
//
//
//        // 字体Wingdings 2的符号
//        TextRenderData selectSymbol = new TextRenderData("R",new Style("Wingdings 2",10));
//        TextRenderData unselectSymbol = new TextRenderData("\u00A3",new Style("Wingdings 2",10));
//        int actionItem = 3;
//        String fieldName = "";
//        for(int i = 0; i < 4; i++) {
//            fieldName = "actionItem" + i;
//            int bitValue = (actionItem >> i) & 0x01;
//            if (bitValue == 1) {
//                dataMap.put(fieldName, selectSymbol);
//            }else {
//                dataMap.put(fieldName, unselectSymbol);
//            }
//        }
//
//
//        selectWordRadio(1, 2, "hasDisease", dataMap);
//
//
//        XWPFTemplate template = XWPFTemplate.compile(wordTemplate).render(
//                dataMap);
//
//        FileOutputStream out = new FileOutputStream(outWordFilePath);
//        template.write(out);
//        out.flush();
//        out.close();
//        template.close();
        Word2PdfUtil.doc2pdf("/Users/songqi/Desktop/fa6ec972-9dce-41a8-b768-7227621dcd1a.docx", "/Users/songqi/Desktop/fa6ec972-9dce-41a8-b768-7227621dcd1a.pdf");
    }

}
